package com.yjhb.android.sbz.v2.fragment.reward

import android.content.Intent
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.tool.EventMsg
import com.yjhb.android.sbz.tool.LoadingDialogTool
import com.yjhb.android.sbz.v2.activity.LastIssueRankActivity
import com.yjhb.android.sbz.v2.adapter.V2TaskRewardRankAdapter
import com.yjhb.android.sbz.v2.entity.TaskRewardItem
import com.yjhb.android.sbz.v2.entity.UserTaskRewardInfo
import com.yjhb.android.sbz.v2.mvp.presenter.ConfigPresenter
import com.yjhb.android.sbz.v2.mvp.presenter.V2RewardPresenter
import com.yjhb.android.sbz.v2.mvp.view.ConfigView
import com.yjhb.android.sbz.v2.mvp.view.V2RewardView
import com.jojo.android.zxlib.base.BaseFragment
import com.jojo.android.zxlib.tool.ZxToast
import com.jojo.android.zxlib.view.dialog.ILoading
import kotlinx.android.synthetic.main.fragment_money_make_reward_v2.*
import kotlinx.android.synthetic.main.fragment_money_make_reward_v2.activity_time_tv
import kotlinx.android.synthetic.main.fragment_money_make_reward_v2.emptyRecyclerView
import kotlinx.android.synthetic.main.fragment_money_make_reward_v2.emptyView
import kotlinx.android.synthetic.main.fragment_money_make_reward_v2.last_rank_tv
import kotlinx.android.synthetic.main.fragment_money_make_reward_v2.now_make_money_tv
import org.greenrobot.eventbus.EventBus
import java.util.*


/**
 *  创建者 zx
 *  创建时间 2021/4/1 15:49
 */
class FragmentMoneyMakeV2 : BaseFragment<V2RewardPresenter<TaskRewardItem, UserTaskRewardInfo>>(),
        V2RewardView<TaskRewardItem, UserTaskRewardInfo>, ConfigView {


    private var adapterV2: V2TaskRewardRankAdapter? = null
    private val data = mutableListOf<TaskRewardItem>()
    private var userInfo: UserTaskRewardInfo? = null
    private var loadingView: ILoading? = null
    private val type = 2

    private val configPresenter = lazy { ConfigPresenter(this) }

    override fun initPresenter(): V2RewardPresenter<TaskRewardItem, UserTaskRewardInfo>? {
        return V2RewardPresenter(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_money_make_reward_v2
    }

    override fun initUI() {
        val calendar = Calendar.getInstance()
        //设置时间
        val time = StringBuilder("活动时间：")
                .append(calendar[Calendar.YEAR])
                .append(".")
                .append(calendar[Calendar.MONTH] + 1)
                .append(".")
                .append("1")
                .append("-")
                .append(calendar[Calendar.YEAR])
                .append(".")
                .append(calendar[Calendar.MONTH] + 1)
                .append(".")
                .append(calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
                .toString()
        activity_time_tv.text = time
        //设置空视图
        emptyView.showLoading()
        emptyRecyclerView.emptyView = emptyView
        activity?.run {
            adapterV2 = V2TaskRewardRankAdapter(this)
            adapterV2
        }?.let {
            it.data = this.data
        }
        emptyRecyclerView.adapter = this.adapterV2
        loadingView = LoadingDialogTool.createDialog(this)

        configPresenter.value.getSysConfig("TaskReward")
    }

    override fun initEvent() {
        emptyView.setOnReloadClickListener {
            presenter.getNewRankList(type, TaskRewardItem::class.java)
        }
        now_make_money_tv.setOnClickListener {
            activity?.finish()
            EventBus.getDefault().post(EventMsg(EventMsg.REQUEST_MAIN_SWITCH_FRAGMENT, 1))
        }
        last_rank_tv.setOnClickListener {
            val intent = Intent(activity, LastIssueRankActivity::class.java)
            intent.putExtra("type",type)
            startActivity(intent)
        }
    }

    override fun initData() {
        presenter.getUserRankInfo(type, UserTaskRewardInfo::class.java)
        presenter.getNewRankList(type, TaskRewardItem::class.java)

    }

    override fun onLoadingList() {
        emptyView.showLoading()
    }

    override fun onLoadingUserInfo() {
        loadingView?.show()
    }

    override fun onListSuccess(period:String,data: List<TaskRewardItem>) {
        this.data.clear()
        this.data.addAll(data)
        adapterV2?.notifyDataSetChanged()
        emptyView.showTips("暂无排行数据")
//        calculate()
    }

    override fun onUserInfoSuccess(userInfo: UserTaskRewardInfo) {
        this.userInfo = userInfo
        task_money_tv?.text = userInfo.recentReward
        task_num_tv?.text = userInfo.recentTaskCount
        task_rank_tv?.text = userInfo.distance
//        calculate()
        loadingView?.cancel()
    }

    override fun onFailure(type: Int, code: Int, msg: String) {
        when (type) {
            1 -> {
                emptyView.showFail(msg, true)
            }
            2 -> {
                loadingView?.cancel()
                ZxToast.showINFOBottomToast(msg)
            }
        }
    }

    override fun onConfig(config: JSONObject?) {
        config?.let {
            reward_title_tv?.text = it.getString("title")
            task_num_title_tv?.text = it.getString("currentTaskCount")
            task_money_title_tv?.text = it.getString("currentTaskReward")
            task_rank_title_tv?.text = it.getString("currentRank")
            banner_tv?.text = it.getString("banner")
            colRank_tv?.text = it.getString("colRank")
            colMoney_tv?.text = it.getString("colMoney")
            colReward_tv?.text = it.getString("colReward")
        }
    }

    override fun onConfig(config: JSONArray?) {

    }

    override fun onFailure(code: Int, msg: String?) {

    }

//    private fun calculate() {
//        for (item in data) {
//            if (item.userId == App.userInfo?.id) {
//                task_rank_tv.text = "${data.indexOf(item) + 1}"
//                break
//            }
//            if (data.indexOf(item) == data.lastIndex) {
//                userInfo?.let {
//                    task_rank_tv.text = "还差${item.reward?.subtract(it.recentReward)}元上榜"
//                }
//            }
//        }
//
//    }

    override fun receiving() {}
    override fun successReceived() {}
}